package com.traffic.trafficLine;


import com.traffic.entity.TrafficLine;
import com.traffic.repository.trafficline.TrafficLineRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
public class TrafficLineServiceImpl implements TrafficLineService {


	@Autowired
	private TrafficLineRepository repository;



	@Override
	@Transactional(rollbackFor = Exception.class, timeout = 30, propagation = Propagation.REQUIRED)
	public void addTrafficLine(TrafficLine t) {
		repository.save(t);
	}

	@Override
	@Transactional(rollbackFor = Exception.class, timeout = 30, propagation = Propagation.REQUIRED)
	public void delTrafficLine(Long id) {
	repository.delete(id);
	}

	@Override
	public void updateTrafficLine(TrafficLine t) {
		repository.save(t);
	}

	@Override
	public List<TrafficLine> findAll() {
		return repository.findAll();
	}

	@Override
	public Page<TrafficLine> findTrafficLine(Pageable page) {
		return repository.findAll(page);
	}

	@Override
	@Transactional(rollbackFor = Exception.class, timeout = 30, propagation = Propagation.REQUIRED)
	public Page<TrafficLine> findTrafficLineByLine(String line, Pageable page) {
		return repository. findAllByLine(line,page);
		//return null;
	}

	@Override
	public List<TrafficLine> findTrafficLineByLine(String line) {
		return repository. findAllByLine(line);
	}

	@Override
	@Transactional(rollbackFor = Exception.class, timeout = 30, propagation = Propagation.REQUIRED)
	public Page<TrafficLine> findTrafficLineByRoad(String road, Pageable page) {
		return repository.findAllByStartroad(road,page);
		//return null;
	}

	@Override
	@Transactional(rollbackFor = Exception.class, timeout = 30, propagation = Propagation.REQUIRED)
	public Page<TrafficLine> findAllByStartroadAndEndroad(String startroad, String endroad, Pageable page) {
		return repository. findAllByStartroadAndEndroad(startroad,endroad,page);


	}

	@Override
	public Page<TrafficLine> findAllByLineOrStartroadOrEndroad(String line, String startroad, String endroad, Pageable page) {
		return repository.findAllByLineOrStartroadOrEndroad(line, startroad,endroad, page);
	}

	@Override
	@Transactional(rollbackFor = Exception.class, timeout = 30, propagation = Propagation.REQUIRED)
	public TrafficLine get(Long id) {
		return repository.findById(id);
	}
}
